
package Control;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import Entity.MsgPackage;
import GUI.EstimationStage_GUI;
import GUI.ScreenManager;
import Entity.EstimationReport;

/**
 * Controls the instance of Estimation Stage Control
 * This class sends and gets data from the server 
 * and display it on the panel.
 * @author Elad
 */
public class EstimationStageControl {
	private EstimationStage_GUI estimationGui;
	private MsgPackage estimationMsg;
	private EstimationReport estimationReport;
	private Boolean result;
	
	/**
	 * the constructor
	 */
	public  EstimationStageControl(){
		estimationGui= new  EstimationStage_GUI();
		estimationMsg= new MsgPackage();
		estimationReport= new EstimationReport();
		
	}
	
	/**
	 * 
	 * @return EstimationStageGui
	 */
	public EstimationStage_GUI getEstimationGui(){
		return estimationGui;
	}
	
	/**
	 * This method sends request to the server
	 *  The server sends an answer (received from query from database) 
	 *  and the control display it in a message
	 */
	public boolean estimation_Submit_pressed(Integer reqID){
		String b= estimationGui.getRequiredChange().getText();
		String c= estimationGui.getProfit().getText();
		String d= estimationGui.getConstraints().getText();
		String e= estimationGui.getHazards().getText();
		String f= estimationGui.getDuration().getText();
		
		if( b.equals("")||c.equals("")||d.equals("")||e.equals("")||f.equals("")){
			JOptionPane.showMessageDialog(null, "Not all the fileds are filled", "ERROR", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		else{
			estimationReport.setR_id(reqID);
			estimationReport.setChangeRequired(b);
			estimationReport.setBenefit(c);
			estimationReport.setConstraints(d);
			estimationReport.setRisks(e);
			estimationReport.setDurationEstimation(Integer.parseInt(f));
			
			estimationMsg.setObject(estimationReport);
			estimationMsg.setOp_code(OP_CODE.SET_ESTIMATION_STAGE);
			ScreenManager.getConnection().accept(estimationMsg);
			
			//while(ScreenManager.getConnection().getAnswerReceivedFlag() == false);	//wait until flag is changed and shows that result was received from server
			//Object o = ScreenManager.getConnection().getResultobject();
			Object o = ScreenManager.getConnection().cclient.getMessage();
			result = (Boolean)o;
			JOptionPane.showMessageDialog(null, "Your stage was updated successfully!!", "", JOptionPane.INFORMATION_MESSAGE);
			return true;
		}
	}
}		
